package com.ivanceras.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.model.Question;
import com.ivanceras.model.Questionnaire;
import com.ivanceras.view.QuestionView;
import com.ivanceras.view.QuestionnaireView;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Multiplechoice implements EntryPoint{
	VerticalPanel questionnairePanel = new VerticalPanel();
	private final SimulationServiceAsync simulationService = GWT
			.create(SimulationService.class);

	public void onModuleLoad() {
		initLayout();
		//getQuestionnaires();
		displayQuestions(new Long(49));
		//saveQuestionnaire();
		addQuestions();
	}
	public void addQuestions(){
		Question qn = new Question();
		qn.text = "Question number 1 of Nursing";
		qn.setQuestionnaireID(49);
		qn.addChoice("choiceA", true);
		qn.addChoice("choiceB", false);
		qn.addChoice("choiceC", false);
		qn.addChoice("choiceD", false);
		simulationService.saveQuestion(qn, new AsyncCallback<Question>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error saving question!");
				
			}

			@Override
			public void onSuccess(Question result) {
				System.out.println("Success saving question!");
			}
			
		});
		
	}
	public void initLayout(){
		RootPanel.get().add(questionnairePanel);
	}
	public void saveQuestionnaire(){
		Questionnaire qnr = new Questionnaire();
		qnr.title = "Nursing Review";
		qnr.description = "For Nursing Review";
		simulationService.saveQuestionnaire(qnr,
				new AsyncCallback<Questionnaire>(){

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error saving questionnaire");
					}

					@Override
					public void onSuccess(Questionnaire qnr) {
						System.out.println("sucess adding questionnaire");
						System.out.println("ID: "+qnr.questionnaireID);
						System.out.println("title: "+qnr.title);
						System.out.println("description: "+qnr.description);
					}
			
		});
	}
	public void getQuestionnaires(){
		simulationService.getQuestionnaires(new AsyncCallback<ArrayList<Questionnaire>>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error!");
			}

			@Override
			public void onSuccess(ArrayList<Questionnaire> result) {
				System.out.println("Questionnaires.....");
				for(Questionnaire qnr : result){
					final QuestionnaireView questionnaireViewer = new QuestionnaireView();
					questionnaireViewer.setQuestionnaire(qnr);
					questionnairePanel.add(questionnaireViewer.view);
					System.out.println("title: "+qnr.title);
					System.out.println("description: "+qnr.description);
					
					questionnaireViewer.view.addClickHandler(new ClickHandler(){

						@Override
						public void onClick(ClickEvent event) {
							System.out.println("Im clicked!"+questionnaireViewer.questionnaire.questionnaireID);
							displayQuestions(questionnaireViewer.questionnaire.questionnaireID);
						}
						
					});
				}
			}
			
		});
	}
	public void displayQuestions(Long questionnaireKey){
		RootPanel.get().clear();
		simulationService.getQuestions(questionnaireKey, new AsyncCallback<Questionnaire>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("There was an error displaying questions");
			}

			@Override
			public void onSuccess(Questionnaire result) {
				System.out.println("result: "+result.title);
				for(Question qn : result.questions){
					QuestionView qnView = new QuestionView();
					RootPanel.get().add(qnView.edit);
					qnView.setQuestion(qn);
					System.out.println(qn.text);
				}
			}
			
		});
	}
}
